function handleKey(input,event){
	if((event.keyCode==13)&&(input.value!="")){
		sendMessage(input.value);
		input.value="";
	}
}

function sendMessage(text){
	var request=new XMLHttpRequest();
	var params="message="+text;
	request.onreadystatechange=function(){
		if(request.readyState==4){
			if(!(request.status==200)){
				alert("internal server error");
			}else{
				//document.getElementById("chat_history").innerHTML+=request.responseText;
			}
		}
	};
	request.open("POST","ChatHandler?reason=sendMessage",true);
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	request.send(params);
}

var lastUpdate=0;
var getMessageSound = new Audio("sounds/message.ogg");
var requestGet=new XMLHttpRequest();
setInterval(function(){
	requestGet.onreadystatechange=function(){
		if(requestGet.readyState==4){
			if(!(requestGet.status==200)){
				//alert("internal server error");
			}else{
				lastUpdate=requestGet.getResponseHeader("lastUpdate");
				if(requestGet.responseText!=""){
					var chatHistory=document.getElementById("chat_history");
					chatHistory.innerHTML+=requestGet.responseText;
					window.scrollTo(0, document.body.scrollHeight);
					getMessageSound.play();
				}
				if((requestGet.getResponseHeader("reload")=="true")&&(parent!=null)){
					var frame=parent.document.getElementById(requestGet.getResponseHeader("reloadFrame"));
					frame.src=frame.src;
				}
			}
		}
	};
	requestGet.open("GET","ChatHandler?reason=sendMessage&lastUpdate="+lastUpdate,true);
	requestGet.send();
}, 500);